- 首先要有一个公众号 
- 获取 AppID 和 AppSecret - 登陆微信公众平台 
- 开发 -> 基本配置,查看 AppID 和 AppSecret 
 
- 配置安全域名 
- 码砖 - index.html 中引入 JS 接口文件 http://res.wx.qq.com/open/js/jweixin-1.2.0.js 
- 获取access_token - https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=AppID&secret=AppSecret
- 返回结果 - 1 
 2
 3
 4- { 
 access_token: AccessToken,
 expires_in: 7200
 }
- AccessToken 有效时长为7200秒,即2小时 
 
- 根据 AccessToken 获取ticket - https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=AccessToken&type=jsapi
- 返回结果1 
 2
 3
 4
 5
 6{ 
 errcode: 0,
 errmsg: "ok",
 ticket: ticket,
 expires_in: 7200
 }
 
- 获取签名signature - 定义时间戳 timestamp = +(new Date)
- 定义随机字符串 noncestr = Math.random() * 10000 + '',必须是字符串,否则在IOS下会出现config:fail
- 当前网页地址 url = window.location.href.split('#')[0]
- 有效的jsapi_ticket jsapi_ticket = ticket
- 获取签名signature1 
 2var shaStr = 'jsapi_ticket=ticket&noncestr=noncestr×tamp=timestamp&url=url' 
 var signature = sha1(shaStr)
 
- 定义时间戳 
- 注入权限验证配置 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- wx.config({ 
 debug: true,
 appId: AppID,
 timestamp: timestamp,
 nonceStr: nonceStr,
 signature: signature,
 jsApiList: [ // 需要使用的接口列表
 ‘onMenuShareTimeline’, // 分享到朋友圈
 ‘onMenuShareAppMessage’, // 分享给朋友
 ‘onMenuShareQQ’ // 分享到QQ
 ]
 })- debug: true调试进行debug,配置成功会自动提示- errmsg: {config:ok}
 
- 通过ready接口处理成功验证 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24- wx.ready(function () { 
 wx.onMenuShareTimeline({
 title: Title,
 link: url,
 imgUrl: imgUrl,
 success: function () {}
 })
 wx.onMenuShareAppMessage({
 title: Title,
 link: url,
 desc: desc,
 imgUrl: imgUrl,
 success: function () {}
 })
 wx.onMenuShareQQ({
 title: Title,
 link: url,
 desc: desc,
 imgUrl: imgUrl,
 success: function () {}
 })
 })
 
- 参考